<?php
/**
 * db_mysql.class.php mysql数据库连接操作类
 *
 * 继承于db.i.class.php接口
 *
 * @author          hyperblue
 * @version         0.1
 * @copyright       (C) 2011- *
 * @license	        http://www.bingceng.com
 * @lastmodify	    2011-08-10 14:50
 */
 
defined('BC_INC_PATH') or exit("Access Denied!");

class db_mysql implements Idb {

	private $_config = array(
		'db_host' => null,
		'db_name' => null,
		'db_user' => null,
		'db_password' => null,
		'db_port' => '3306',
		'db_charset' => 'utf8',
		'db_prefix' => '',
		'db_type' => 'mysql'
	);

	protected $_link;

	/**
	 * 构造函数
	 */
	public function __construct($config = null) {
		$this->_config = $config;
		if ($this->_config) $this->_connect();
		else die('Please check database configuration!');
	}

	/**
	 * 数据库连接
	 */
	public function _connect() {
		$this->_link = @mysql_connect($this->_config['db_host'],$this->_config['db_user'],$this->_config['db_password']) or die('Could not connect: ' . mysql_error());
		
		mysql_select_db($this->_config['db_name']) or die('Haven\'t found this database: ' . mysql_error());
		
		if ($this->_config['db_charset']) $this->query('set names '.$this->_config['db_charset']);
	}
	
	/**
	 * SQL查询
	 *
	 * @param $sql string
	 * @return resource
	 */
	public function query($sql) {
		$sql = F_sqls($sql);
		return mysql_query($sql, $this->_link);
	}
	
	/**
	 * 自定义查询
	 *
	 * @param $table string
	 * @param $column string
	 * @param $condition string
	 */
	public function select($table, $column = '*', $condition = '') {
		if (!$talbe) die('Please choose a table!');
		$sql = 'SELECT '. $clonum .' FROM '. $this->_config['db_prefix'] . $table . $condition;
		return $this->getRows($sql);	
	}
	
	/**
	 * 获取一行记录
	 *
	 * @param $sql string
	 */
	public function getOne($sql) {
		$row = array();
		if ($sql) {
			$sql = F_sqls($sql);
			$result = $this->query($sql);
			if (is_resource($result)) {
				$row = mysql_fetch_assoc($result); 
				@mysql_free_result($result);
			}
		}
		
		return $row;
	}
	
	/**
	 * 获取多行记录
	 *
	 * @param $sql string
	 */
	public function getRows($sql) {
		$rows = array();
		if ($sql) {
			$sql = F_sqls($sql);
			$result = $this->query($sql);
			if (is_resource($result)) {
				while(($rs = mysql_fetch_assoc($result)) != false) {
					$rows[] = $rs;
				}
				@mysql_free_result($result);
			}
		}
		
		return $rows;
	}
	
	/**
	 * 执行更新和添加操作
	 *
	 * @param $operate string 'UPDATE/INSERT/DELETE'
	 * @param $table string
	 * @param $field_values array   like array('column_1'=>'column_1_value', 'column_2'=>'column_2_value');
	 * @param $condition string
	 * @return boolean
	 */
	public function execute($operate = 'UPDATE', $table, $field_values = array(), $condition = '') {
		$str = '';
		$fields = '';
		$values = '';
	
		if (!count($field_values) && $operate != 'DELETE') return false;
		switch ($operate) {
			case 'UPDATE':
				foreach ($field_values as $field => $value) {
					$str .= '`'. strip_tags($field) .'` = \''. addslashes($value) .'\', ';
				}
				$str = substr($str, 0, strlen(trim($str))-1);
				$sql = 'UPDATE '. $this->_config['db_prefix'] . $table . ' SET ' . $str . $condition;
				break;
			case 'INSERT':
				foreach ($field_values as $field => $value) {
					$str = '`'. $field .'` = \''. addslashes($value) .'\', ';
					$fields .= '`'. addslashes($field) .'`, ';
					$values .= '\''. addslashes($value) .'\', ';
				}
				$fields = substr($fields, 0, strlen(trim($fields))-1);
				$values = substr($values, 0, strlen(trim($values))-1);
				$sql = 'INSERT INTO '. $this->_config['db_prefix'] . $table . ' (' . $fields . ') VALUES (' . $values . ')';
				break;
			case 'DELETE':
				$sql = 'DELETE FROM '. $this->_config['db_prefix'] . $table . $condition;
				break;
			default:
				die('Please change your operate to UPDATE or INSERT!');
				break;
		}

		$result = $this->query($sql);
		if (is_resource($result)) return true;
	}
	
	/**
	 * 关闭连接
	 */
	public function _close() {
		if ($this->_link) mysql_close($this->_link);
	}
	
	public function __destruct() {
		$this->_close();
	}
}

?>
